
open Ast
open Format




let _ =
	let filename = if Array.length Sys.argv > 1 then Sys.argv.(1) else "-" in
	let input = if filename = "-" then stdin else open_in filename in
	let lexbuf = Lexing.from_channel input in
	try
		let p = Parser.prog Lexer.token lexbuf in
		print_string
			("Ext.onReady(function(){new Ext.Viewport(\n  " ^ (Json.json_to_string "  " (Gen.program_to_json p)) ^ "\n)});")
	with
	  | Parsing.Parse_error ->
		let p = lexbuf.Lexing.lex_curr_p in
		let l = p.Lexing.pos_lnum in
		let c = p.Lexing.pos_cnum - p.Lexing.pos_bol in
		fprintf err_formatter "%s: Parser error at line %d char %d@." filename l c

	  | Lexer.LexError ->
			fprintf err_formatter "%s, characters %d-%d: unexpected symbol '%s'@."
				filename
				(Lexing.lexeme_start lexbuf)
				(Lexing.lexeme_end lexbuf)
				(Lexing.lexeme lexbuf)
	  
(*
let _ =
	print_string "\n"
*)

